package com.finger.gulimall.order.listener;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import com.alipay.api.AlipayApiException;
import com.finger.gulimall.order.config.AlipayTemplate;
import com.finger.gulimall.order.entity.vo.PayAsyncVo;
import com.finger.gulimall.order.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName PayedNotifyListener
 * @Description
 * @Author finger
 * @Date 2023/5/3 11:11
 * @Version 1.0
 */
@Controller
public class PayedNotifyListener {

	@Autowired
	PaymentInfoService paymentInfoService;

	@Autowired
	AlipayTemplate alipayTemplate;

	@ResponseBody
	@RequestMapping("/payed/notify")
	public String payedNotify(PayAsyncVo payAsyncVo,HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
//		Map<String, String[]> map =
//				request.getParameterMap();
//		for (Map.Entry<String, String[]> entry : map.entrySet()) {
//			System.out.println(entry.getKey()+"-->"+ Arrays.toString(entry.getValue()));
//		}

		//获取支付宝POST过来反馈信息
//		Map<String,String> params = new HashMap<String,String>();
//		Map<String,String[]> requestParams = request.getParameterMap();
//		for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
//			String name = (String) iter.next();
//			String[] values = (String[]) requestParams.get(name);
//			String valueStr = "";
//			for (int i = 0; i < values.length; i++) {
//				valueStr = (i == values.length - 1) ? valueStr + values[i]
//						: valueStr + values[i] + ",";
//			}
//			//乱码解决，这段代码在出现乱码时使用
//			valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
//			params.put(name, valueStr);
//		}

		return paymentInfoService.payedNotify(payAsyncVo);
		// 验参，看传过来的参数是否有人动过
//		boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayTemplate.getAlipay_public_key(), alipayTemplate.getCharset(), alipayTemplate.getSign_type()); //调用SDK验证签名
//
//		if(signVerified){
//
//		}else{
//			return "error";
//		}
	}
}
